package com.itheima.ssm.service.impl;

import com.itheima.ssm.dao.IRoleDao;
import com.itheima.ssm.domain.Pemission;
import com.itheima.ssm.domain.Role;
import com.itheima.ssm.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IRoleServiceImpl implements IRoleService {

    @Autowired
    private IRoleDao iRoleDao;


    @Override
    public List<Role> findAll() throws Exception{
        return iRoleDao.findAll();
    }

    @Override
    public void save(Role role) throws Exception {
        iRoleDao.save(role);
    }

    /**
     * 先删除外键关联，在删除实体数据
     * @param roleId
     * @throws Exception
     */
    @Override
    public void deleteRoleById(int roleId) throws Exception {
        iRoleDao.deletePermissionByRoleId(roleId);
        iRoleDao.deleteUserIdByRoleId(roleId);
        iRoleDao.deleteRole(roleId);
    }

    @Override
    public Role findById(String roleId) throws Exception {
        return iRoleDao.findById(roleId);
    }

    @Override
    public List<Pemission> findOtherPermission(String roleId) throws Exception {
        return iRoleDao.findOtherPermission(roleId);
    }

    @Override
    public void addPermissionToRole(String roleId, String[] pids) throws Exception {
        for (String pid : pids) {
            iRoleDao.addPermissionToRole(roleId,pid);
        }
    }
}
